<?php

class Myfb_Render {

  /**
   *
   * @var Render
   */
  protected static $_blocks;
  protected static $_layout;
  protected static $_theme;

  /**
   *
   * @param int    $myfb_id
   * @param string $fb_app_id
   */
  public static function setup($myfb_id, $fb_app_id) {
    $sql = "
     insert into
     yns_myfb_page_widget
        (myfb_id, widget_id, status, priority, block_name, params)
      select $myfb_id as myfb_id,
      widget_id,
      status,
      priority,
      block_name,
      params
    from yns_myfb_page_setup
    where
    fb_app_id =  '$fb_app_id'
    ;
    ";
    $db = db::factory();
    $db->query($sql);
    $sql = "insert ignore into yns_myfb_page_layout (myfb_id, layout_id) values ($myfb_id, 1);";
    $db->query($sql);
  }

  /**
   * @param  action|widget $action
   * @return string
   */
  public static function toMaster(&$action) {
    if ($tpl = $action->getParam('master')) {
      return $action->render($tpl);
    }
    return self::toLayout($action);
  }

  /**
   *
   * @param  action|widget $action
   * @return string
   */
  public static function toLayout(&$action) {
    if ($tpl = $action->getParam('layout')) {
      return $action->render($tpl);
    }
    return self::toView($action);
  }

  public static function toView(&$action) {
    if (($tpl = $action->getParam('view')) == NULL) {
      $tpl = str_replace('_', '.', get_class($action));
    }
    return $action->render($tpl);
  }

  /**
   * @param string $name
   * @param array $args optional
   */
  public static function render_widget($name, $args = NULL) {
    $class_name = str_replace('.', '_', $name);
    $widget = new $class_name();
    $widget->do_get($args);
    echo render::toView();
  }

  /**
   *
   * @param mixed $myfb_id
   */
  protected static function _boot($myfb_id) {
    $db = Db::factory();
    $sql = "
      select p.*, w.widget_class from yns_myfb_page_widget as p
      left join yns_myfb_widget as w on( p.widget_id = w.widget_id)
      where p.status = 1 and myfb_id = $myfb_id
      order by priority
      ";
    if ($rows = $db->fetchAll($sql)) {
      foreach ($rows as $row) {
        self::$_blocks[$row['block_name']][] = $row;
      }
    }
    $sql = "select *
      from yns_myfb_page_layout as p
      left join yns_myfb_layout as l on (p.layout_id = l.layout_id)
      where p.myfb_id = ?
      limit 1
      ;";
    $row = $db->fetchRow($sql, $myfb_id);
    self::$_layout = $row['layout_path'];
  }

  public static function getEditLayoutPath() {
    $layout = self::getLayout();
    return AEngine::getPath($layout . '.Edit');
  }

  /**
   *
   * @param setting $myfb_id
   */
  public static function boot(&$action) {
    if (self::$_blocks == NULL) {
      $myfb_id = myfb::getId($action->getAppId());
      $action->setMyfbId($myfb_id);
      self::_boot($myfb_id);
      $action->setParam('layout', self::getLayout());
    }
  }
  
  protected static function getBlock($block_name) {
    return @self::$_blocks[$block_name] ? self::$_blocks[$block_name] : array();
  }

  public static function getLayout() {
    return self::$_layout ? self::$_layout : 'Layout.All.A1';
  }

  /**
   *
   * @param string $block_name
   */
  public static function renderBlock($block_name) {
    foreach (self::getBlock($block_name) as $row) {
      $real_name = @$row['widget_class'];
      $params = unserialize($row['params']);
      $class_name = str_replace('.', '_', $real_name);
      $widget = new $class_name($params);
      $widget->doRequest();
      echo $widget->render($real_name);
      unset($widget);
    }
  }

  /**
   *
   * @param string $real_name
   * @param array|NULL $args
   */
  public static function renderWidget($real_name, $args= array()) {
    $class_name = str_replace('.', '_', $real_name);
    $widget = new $class_name($args);
    $widget->doRequest();
    echo $widget->render($real_name);
  }

  /**
   *
   * @param string $block_name
   */
  public static function renderEditBlock($block_name) {
    echo '<div class="ui-block-container" block="' . $block_name . '">';
    $format = '
      <div class="portlet" wid="%s" id="id_widget_%s">
        <div class="portlet-header">Feeds<a onclick="layoutMag.edit(this)" class="uiLinkEditWidget">[Edit]</a></div>
        <div class="portlet-content">%s</div>
      </div>
    ';
    foreach (self::getBlock($block_name) as $row) {
      $real_name = @$row['widget_class'];
      $class_name = str_replace('.', '_', $real_name);

      $params = @unserialize($row['params']);
      $widget = new $class_name($params);

      $widget->doRequest();
      $result = $widget->render($real_name);
      echo sprintf($format, $row['widget_id'], $row['id'], $result);
      unset($widget);
    }
    echo '</div>';
  }

}
